package com.tianji.learning.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.common.domain.query.PageQuery;
import com.tianji.learning.domain.dto.LearningPlanDTO;
import com.tianji.learning.domain.vo.LearningLessonVO;
import com.tianji.learning.domain.vo.LearningPlanPageVO;
import com.tianji.learning.service.ILearningLessonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 学生课程表 前端控制器
 * </p>
 *
 * @author rui
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/lessons")
@Api(tags = "课表相关接口")
@RequiredArgsConstructor
public class LearningLessonController {
    private final ILearningLessonService learningLessonService;
    @GetMapping("/page")
    @ApiOperation("分页查询我的课表")
    public PageDTO<LearningLessonVO> pageQuery(PageQuery pageQuery){
        return learningLessonService.pageQuery(pageQuery);
    }
    @GetMapping("/now")
    @ApiOperation("查询正在学习的课程")
    public LearningLessonVO getByNow(){
        return learningLessonService.getByNow();
    }
    @DeleteMapping("/{courseId}")
    @ApiOperation("删除对应课程")
    public void deleteByCourseId(@PathVariable Long courseId){
        learningLessonService.deleteByCourseId(courseId);
    }

    /**
     * 校验当前用户是否可以学习当前课程
     * @param courseId 课程id
     * @return lessonId，如果是报名了则返回lessonId，否则返回空
     */
    @GetMapping("/{courseId}/valid")
    @ApiOperation("校验当前用户是否可以学习当前课程")
    Long isLessonValid(@PathVariable("courseId") Long courseId){
        return learningLessonService.isLessonValid(courseId);
    }
    @GetMapping("/{courseId}")
    @ApiOperation("根据课程id查询课表是否有该课程")
    public LearningLessonVO getByCourseId(@PathVariable("courseId") Long courseId){
        return learningLessonService.getByCourseId(courseId);
    }
    /**
     * 统计课程学习人数
     * @param courseId 课程id
     * @return 学习人数
     */
    @GetMapping("/{courseId}/count")
    @ApiOperation("统计课程学习人数")
    Integer countLearningLessonByCourse(@PathVariable("courseId") Long courseId){
        return learningLessonService.countLearningLessonByCourse(courseId);
    }
    @ApiOperation("创建学习计划")
    @PostMapping("/plans")
    public void createLearningPlans(@Valid @RequestBody LearningPlanDTO planDTO){
        learningLessonService.createLearningPlan(planDTO.getCourseId(), planDTO.getFreq());
    }
    @ApiOperation("查询学习计划进度")
    @GetMapping("/plans")
    public LearningPlanPageVO queryLearningPlans(@Valid PageQuery pageQuery){
        return learningLessonService.queryLearningPlans(pageQuery);
    }
}
